package com.pango.system.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pango.core.easyui.json.ResultMessage;
import com.pango.core.easyui.support.UISupportUtils;
import com.pango.core.easyui.vo.ComboTree;
import com.pango.core.easyui.vo.ComboTreeModel;
import com.pango.core.paginator.support.PageForm;
import com.pango.core.security.UserContext;
import com.pango.core.security.UserDetails;
import com.pango.system.entity.ResourceEntity;
import com.pango.system.model.Resource;
import com.pango.system.model.Role;
import com.pango.system.model.RoleResourcePermission;
import com.pango.system.model.User;
import com.pango.system.services.ResourceService;
import com.pango.system.services.RoleService;

/**
 * ClassName: RoleController <br/>
 * @author   thierry.fu
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping("/roleController")
public class RoleController {
	
	@Autowired
	private ResourceService resourceService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping(params = "list")
	public String list() {
		return "role/list";
	}
	
	@RequestMapping(params = "fun")
	public String fun(HttpServletRequest request) {
		String roleId = request.getParameter("roleId");
		request.setAttribute("roleId", roleId);
		return "role/roleSet";
	}

	@RequestMapping(params = "setAuthority")
	@ResponseBody
    public List<ComboTree> userList(Role role, HttpServletRequest request, ComboTree comboTree) {
		//查询所有的资源列表
		List<ResourceEntity> allResources = new ArrayList<ResourceEntity>();
		if(comboTree.getId() == null){
			allResources = resourceService.selectResourcesByParentId(null);
		}
		else {
			allResources = resourceService.selectResourcesByParentId(Long.parseLong(comboTree.getId()));
		}
		
		UserDetails user = UserContext.getCurrentUser();
		//已经注册的资源
    	List<ResourceEntity> regResources = 
    			resourceService.selectResourcesByUserName(user.getUsername());
    	
    	List<ComboTree> comboTrees = new ArrayList<ComboTree>();
    	
    	ComboTreeModel comboTreeModel = new ComboTreeModel("id", "name", "childList");
    	comboTrees = UISupportUtils.ComboTree(allResources, comboTreeModel, regResources);
    	return  comboTrees;
    }
	
	@RequestMapping(params = "updateAuthority")
	@ResponseBody
	public ResultMessage update(HttpServletRequest request) {
		ResultMessage msg = new ResultMessage();
		Long roleId =Long.parseLong(request.getParameter("roleId"));
		String roleResources = request.getParameter("roleResources");
		//过滤重复functionId
		String[] resources = roleResources.split(",");
		Set<String> resSets = new HashSet<String>();
		for (String res : resources) {
			resSets.add(res);
		}
		//更新角色资源中间表
		List<RoleResourcePermission> records = new ArrayList<RoleResourcePermission>();
		for (String resId : resSets) {
			RoleResourcePermission record = new RoleResourcePermission();
			record.setRoleId(roleId);
			record.setResourceId(Long.parseLong(resId));
			records.add(record);
		}
		//1.0 删除
		resourceService.delRoleResourcesById(roleId);
		//2.0 新增
		resourceService.addRoleResourcesBatch(records);
    	
		return msg;
	}
	
    @ResponseBody
    @RequestMapping(params = "role")
    public List<Role> userView(User user,PageForm pageform) {
    	return roleService.getAll();
    }
    
}
